<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="4" :md="12">
            <el-form-item label="用户昵称:">
              <el-input clearable v-model="where.nickname" placeholder="请输入用户昵称" />
            </el-form-item>
          </el-col>

          <el-col :lg="5" :md="12">
            <el-form-item label="用户ID:">
              <el-input clearable v-model="where.id" placeholder="请输入用户ID" />
            </el-form-item>
          </el-col>

<!--          <el-col :lg="4" :md="12">-->
<!--            <el-form-item label="用户类型:">-->
<!--              <el-select clearable v-model="where.platform" placeholder="请选择类型" class="ele-fluid">-->
<!--                <el-option label="全部" value="" />-->
<!--                <el-option label="ios" value="1" />-->
<!--                <el-option label="安卓" value="2" />-->
<!--                <el-option label="pc" value="3" />-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

          <el-col :lg="5" :md="12">
            <el-form-item label="手机号:">
              <el-input clearable v-model="where.phone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="用户状态:">
              <el-select clearable v-model="where.status" placeholder="请选择类型" class="ele-fluid">
                <el-option label="全部" value="" />
                <el-option label="未注销" value="0" />
                <el-option label="已注销" value="1" />
                <el-option label="已实名" value="2" />
                <el-option label="未实名" value="3" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <el-form-item label="创建时间:">
              <el-date-picker style="width: 100%" v-model="where.create_time" type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss" label-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
                start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="9" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button size="small" type="success" icon="el-icon-download" class="ele-btn-icon" @click="exportExcel"
            v-if="permission.includes('sys:box-recharge-order:export')">导出
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link type="primary" :underline="false" @click="openEdit(row)" v-if="!row.ban_time">封禁登陆
          </el-link>

          <el-popconfirm v-if="row.ban_time" class="ele-action" title="确定要解除封禁吗？" @confirm="unban(row)">
            <el-link type="warning" slot="reference" :underline="false">解除封禁
            </el-link>
          </el-popconfirm>

          <el-popconfirm v-if="row.ban_comment_time" class="ele-action" title="确定要解除禁言吗？" @confirm="uncommentban(row)">
            <el-link type="info" slot="reference" :underline="false">解除禁言
            </el-link>
          </el-popconfirm>

          <el-popconfirm v-if="!row.role" class="ele-action" title="确定要设为官方账号吗？" @confirm="editRole(row)">
            <el-link type="primary" slot="reference" :underline="false">设为官方
            </el-link>
          </el-popconfirm>

          <el-popconfirm v-if="row.role === 1" class="ele-action" title="确定要解除官方账号吗？" @confirm="secureRole(row)">
            <el-link type="danger" slot="reference" :underline="false">解除官方
            </el-link>
          </el-popconfirm>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <el-switch v-model="row.status" @change="editStatus(row)" :active-value="1" :inactive-value="2" />
        </template>

        <template slot="platform" slot-scope="{ row }">
          <el-tag v-if="row.platform === 1" type="warning" size="small">ios</el-tag>
          <el-tag v-if="row.platform === 2" type="success" size="small">安卓</el-tag>
          <el-tag v-if="row.platform === 3" type="" size="small">pc</el-tag>
        </template>

        <template slot="sex" slot-scope="{ row }">
          <el-tag v-if="row.sex === 0" type="warning" size="small">未知</el-tag>
          <el-tag v-if="row.sex === 1" type="success" size="small">男</el-tag>
          <el-tag v-if="row.sex === 2" type="" size="small">女</el-tag>
        </template>

        <template slot="reason" slot-scope="{ row }">
          <div v-if="row.ban_time">
            <div>原因：{{ row.ban_rule }}</div>
            <div>解封剩余：{{ row.ban_time }}</div>
          </div>
        </template>

        <template slot="avatar" slot-scope="{ row }">
          <el-image style="width: 30px; height: 30px" :src="row.avatar" fit="fit"></el-image>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <role-edit :data="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import RoleEdit from "./role-edit.vue";
import { downloadExcel } from "@/utils/excel-export"

export default {
  name: "ReportConfig",
  components: { RoleEdit },
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      // 表格数据接口
      url: "/memberInfo/index",
      // 表格列配置
      columns: [
        {
          prop: "id",
          label: "ID",
          minWidth: 120,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: "mid",
          label: "用户ID",
          minWidth: 120,
          align: "center",
          showOverflowTooltip: true
        },
        {
          prop: "username",
          label: "用户账号",
          minWidth: 160,
          align: "center",
          showOverflowTooltip: true
        },
        {
          prop: "nickname",
          label: "用户昵称",
          minWidth: 160,
          align: "center",
          showOverflowTooltip: true
        },
        {
          prop: "avatar",
          label: "头像",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200,
          slot: "avatar"
        },
        // {
        //   prop: "platform",
        //   label: "用户类型",
        //   align: "center",
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   slot: "platform"
        // },
        {
          prop: "phone",
          label: "绑定手机号",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200,
          formatter: (row, column, cellValue) => {
            return this.$formatDataPhone(cellValue);
          }
        },
        {
          prop: "sex",
          label: "性别",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200,
          slot: "sex"
        },

        {
          prop: "bind_wechat",
          label: "绑定微信",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200,
          formatter: (row, column, cellValue) => {
            if (cellValue == 0) {
              return "未绑定";
            } else if (cellValue == 1) {
              return "已绑定";
            }
          }
        },
        {
          prop: "log_off",
          label: "用户状态",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200,
          formatter: (row, column, cellValue) => {
            if (cellValue == 0) {
              return "未注销";
            } else if (cellValue == 1) {
              return "申请注销";
            } else if (cellValue == 2) {
              return "已注销";
            }
          }
        },
        {
          prop: "age_status",
          label: "实名状态",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200,
          formatter: (row, column, cellValue) => {
            if (cellValue == 0) {
              return "未实名";
            } else if (cellValue == 1) {
              return "成年";
            } else if (cellValue == 2) {
              return "未成年";
            }
          }
        },

        {
          prop: "birthday",
          label: "生日",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: "spend_amount",
          label: "消费金额(单位：元)",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 200
        },
        // {
        //   prop: "score",
        //   label: "用户积分",
        //   align: "center",
        //   showOverflowTooltip: true,
        //   minWidth: 100
        // },
        // {
        //   prop: "ban_rule",
        //   label: "封禁原因",
        //   align: "center",
        //   showOverflowTooltip: true,
        //   minWidth: 250,
        //   slot: "reason"
        // },

        {
          prop: "create_time",
          label: "注册时间",
          sortable: "custom",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 160,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        // {
        //   columnKey: "action",
        //   label: "操作",
        //   width: 230,
        //   align: "center",
        //   resizable: false,
        //   slot: "action",
        //   fixed: "right"
        // }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showAuth: false
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示分配权限 */
    openAuth(row) {
      this.current = row;
      this.showAuth = true;
    },
    /* 删除封禁 */
    unban(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/banLog/edit", { member_id: row.mid, type: 1 })
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 删除禁言 */
    uncommentban(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/banLog/edit", { member_id: row.mid, type: 2 })
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的数据吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/boxBanConfig/delete", { id: this.selection.map(d => d.id) })
            .then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        id: row.id,
        status: row.status
      });
      this.$http
        .post("/boxBanConfig/status", params)
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
          } else {
            row.status = !row.status ? 1 : 0;
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 设为官方账号 */
    editRole(row) {
      const loading = this.$loading({ lock: true });
      const data = {
        id: row.id,
        role: 1
      };
      this.$http
        .post("/memberInfo/editRole", { data })
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 解除官方账号 */
    secureRole(row) {
      const loading = this.$loading({ lock: true });
      const data = {
        id: row.id,
        role: 0
      };
      this.$http
        .post("/memberInfo/editRole", { data })
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 下载数据Excel */
    exportExcel() {
      downloadExcel(`MemberInfo/export`, this.where)
    },
  }
};
</script>

<style scoped></style>
